package cn.com.egova.mahjong.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.com.egova.mahjong.bean.MahjongBO;
import cn.com.egova.mahjong.db.DBHelper;

public class MahjongDao {
    private SQLiteDatabase db;
    private String tableName;

    public MahjongDao(String typeName) {
        this.tableName = getTableName(typeName);
        this.db = DBHelper.getWritableDatabase();
    }

    private String getTableName(String typeName) {
        String tableName = null;
        if ("基本听牌".equals(typeName)) {
            tableName = "BasicTing";
        } else if ("基础训练".equals(typeName)) {
            tableName = "BasicPractice";
        } else if ("进阶训练".equals(typeName)) {
            tableName = "AdvancedPractice";
        } else if ("典型牌例".equals(typeName)) {
            tableName = "TypicalExample";
        }
        return tableName;
    }

    public int getMaxId() {
        Cursor cursor = db.query(tableName, new String[]{"_id"}, null, null, null, null, null);
        return cursor.getCount();
    }


    public boolean add(MahjongBO bo) {
        ContentValues values = new ContentValues();
        values.put("_id", bo.getId());
        values.put("TYPE", bo.getType());
        values.put("DISPLAY_ORDER", bo.getDisplayOrder());
        values.put("WAN", bo.getWan());
        values.put("TIAO", bo.getTiao());
        values.put("TONG", bo.getTong());
        values.put("FENG", bo.getFeng());
        values.put("TING", bo.getTing());
        long res = db.insert(tableName, null, values);
        return res > 0;
    }


    public boolean delete(MahjongBO bo) {
        int res = db.delete(tableName, "_id=?", new String[]{bo.getId() + ""});
        return res > 0;
    }


    public boolean update(MahjongBO bo) {
        ContentValues values = new ContentValues();
        values.put("WAN", bo.getWan());
        values.put("TIAO", bo.getTiao());
        values.put("TONG", bo.getTong());
        values.put("FENG", bo.getFeng());
        values.put("TING", bo.getTing());
        int res = db.update(tableName, values, "_id=?", new String[]{bo.getId() + ""});
        return res > 0;
    }


    public List<MahjongBO> query() {
        List<MahjongBO> list = new ArrayList<>();
        Cursor cursor = db.query(tableName, new String[]{"*"}, null, null, null, null, null);
        while (cursor.moveToNext()) {
            int _id = cursor.getInt(cursor.getColumnIndex("_id"));
            String type = cursor.getString(cursor.getColumnIndex("TYPE"));
            int order = cursor.getInt(cursor.getColumnIndex("DISPLAY_ORDER"));
            String wan = cursor.getString(cursor.getColumnIndex("WAN"));
            String tiao = cursor.getString(cursor.getColumnIndex("TIAO"));
            String tong = cursor.getString(cursor.getColumnIndex("TONG"));
            String feng = cursor.getString(cursor.getColumnIndex("FENG"));
            String ting = cursor.getString(cursor.getColumnIndex("TING"));
            MahjongBO bo = new MahjongBO(_id, type, order, wan, tiao, tong, feng, ting);
            list.add(bo);
        }
        cursor.close();
        return list;
    }


    public MahjongBO queryById(int id) {
        MahjongBO bo = null;
        Cursor cursor = db.query(tableName, new String[]{"*"}, "_id=?", new String[]{id + ""}, null, null, null);
        if (cursor.moveToFirst()) {
            int _id = cursor.getInt(cursor.getColumnIndex("_id"));
            String type = cursor.getString(cursor.getColumnIndex("TYPE"));
            int order = cursor.getInt(cursor.getColumnIndex("DISPLAY_ORDER"));
            String wan = cursor.getString(cursor.getColumnIndex("WAN"));
            String tiao = cursor.getString(cursor.getColumnIndex("TIAO"));
            String tong = cursor.getString(cursor.getColumnIndex("TONG"));
            String feng = cursor.getString(cursor.getColumnIndex("FENG"));
            String ting = cursor.getString(cursor.getColumnIndex("TING"));
            bo = new MahjongBO(_id, type, order, wan, tiao, tong, feng, ting);
        }
        return bo;
    }

    public MahjongBO getPreData(MahjongBO bo) {
        int id = bo.getId();
        if (id > 1) {
            return queryById(id - 1);
        }
        return null;
    }

    public MahjongBO getNextData(MahjongBO bo) {
        int maxId = getMaxId();
        int id = bo.getId();
        if (id < maxId) {
            return queryById(id + 1);
        }
        return null;
    }
}
